package com.example.grape_effect.gl.common

import android.opengl.GLES20
import com.example.grape_effect.gl.glHelper.ShaderHelper
import java.util.*

abstract class GLProgram {
    companion object {
        const val invalidTimeValue = -1L

        // time mode，draw的时候设置为开始时间，后续会根据duration计算progress和当前位置，可看方法updateCurPositionByTime
        const val ANIMATION_MODE_BY_SET_TIME_SELF = 0

        // set progress 外面调用者自己设置progress，可以看updateCurPosition的用法，外界设置progress以及起始点和终点
        const val ANIMATION_MODE_BY_SET_TIME = 1

        fun clearColor() {
            GLES20.glClearColor(0f, 0f, 0f, 0f)
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT)
        }
    }

     var name = ""
     var program = ShaderConstant.INVALID_PROGRAM
     var parameters = HashMap<String, ShaderParameter>()

     abstract val vertexShader: String?
     abstract val fragmentShader: String?

     abstract fun addParameters()
     abstract fun setDefaultParameterValues()
     open fun setParametersToShader() {
        for ((_, value) in parameters) {
            value.setValueToShader()
        }
    }

    // must use in glTread
     open fun initProgram() {
        if (program == ShaderConstant.INVALID_PROGRAM) {
            program = ShaderHelper.Companion.buildProgram(vertexShader!!, fragmentShader!!)
            for ((_, value) in parameters) {
                value.initLocation(program)
            }
        }
    }

    abstract fun draw()




    init {
        addParameters()
        setDefaultParameterValues()
    }
}